package easyexcel.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.ReadListener;
import easyexcel.entity.*;
import easyexcel.listener.HospitalListener;
import easyexcel.listener.MedicaidCodeListener;
import easyexcel.listener.RxListener;
import easyexcel.listener.UserListener;
import easyexcel.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * @description:
 * @author: jichao
 * @date: 2021/4/23 15:05
 */
@Service
public class ExportExcel {

    @Resource
    UserMapper userMapper;

    @Resource
    UserListener userListener;

    @Resource
    HospitalListener hospitalListener;

    @Resource
    MedicaidCodeListener medicaidCodeListener;

    private static final Logger LOGGER = LoggerFactory.getLogger(ExportExcel.class);

    @Resource
    RxListener rxListener;

    public void excelExport(String name, HttpServletResponse response){
        List<User> userList = userMapper.findUsers(name);

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyExcel没有关系
        try {
            String fileName = URLEncoder.encode("用户数据", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 写数据
            EasyExcel.write(response.getOutputStream(), User.class).sheet("用户表")
                    .doWrite(userList);
        } catch (Exception e) {
            LOGGER.error("导出错误");
        }
    }

    /**
     *rx
     */
    public String excelUpload(MultipartFile file){
        String response=null;
        try{
            EasyExcel.read(file.getInputStream(), Rx.class, userListener).sheet().doRead();
            response= "success";
        }catch (Exception e) {
            LOGGER.error("{}",e.getMessage());
        }
        return response;
    }

    /**
     *rxItem
     */
    public String excelUploadAnother(MultipartFile file){
        String response=null;
        try{
            EasyExcel.read(file.getInputStream(), RxItem.class, rxListener).sheet().doRead();
            response= "success";
        }catch (Exception e) {
            LOGGER.error("{}",e.getMessage());
        }
        return response;
    }

    public String drugUpdate(MultipartFile file){
        String response=null;
        try{
            EasyExcel.read(file.getInputStream(), MedicaidCode.class, medicaidCodeListener).sheet().doRead();
            response= "success";
        }catch (Exception e) {
            LOGGER.error("{}",e.getMessage());
        }
        return response;
    }

    /**
     *hospital
     */
    public String excelUploadHospital(MultipartFile file){
        String response=null;
        try{
            EasyExcel.read(file.getInputStream(), Hospital.class, hospitalListener).sheet().doRead();
            response= "success";
        }catch (Exception e) {
            LOGGER.error("{}",e.getMessage());
        }
        return response;
    }

    /**
     *drugCheck
     */
    public String drugCheck(MultipartFile file){
        String response=null;
        try{
            EasyExcel.read(file.getInputStream(), Drug.class, hospitalListener).sheet().doRead();
            response= "success";
        }catch (Exception e) {
            LOGGER.error("{}",e.getMessage());
        }
        return response;
    }

}
